<template>
  <transition name='slide'>
    <music-list :rank="rank" :name="top.topTitle" :image="top.picUrl" :songList="songList"></music-list>
  </transition>
</template>

<script>
import MusicList from '../base/musiclist'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      params: {
        type: 'top',
        song_begin: 0,
        song_num: 100,
        g_tk: 5381,
        loginUin: 0,
        hostUin: 0,
        format: 'json',
        inCharset: 'utf8',
        outCharset: 'utf-8',
        notice: 0,
        platform: 'yqq.json',
        needNewCode: 0
      },
      songList: [],
      rank: true
    }
  },
  created() {
    this.getTopList()
  },
  computed: {
    ...mapGetters(['top']),
    date() {
      let date = new Date()
      let day = date.getDate()
      let month = date.getMonth() + 1
      let year = date.getFullYear()
      return `${year}-0${month}-${day}`
    },
    stage() {
      let day =
        Math.ceil(
          (new Date() - new Date(new Date().getFullYear().toString())) /
            (24 * 60 * 60 * 1000)
        ) + 1
      let date = new Date()
      let year = date.getFullYear()

      return `${year}_${Math.floor(day / 7)}`
    }
  },
  methods: {
    getTop(date) {
      this.axios
        .get('/api/v8/fcg-bin/fcg_v8_toplist_cp.fcg?', {
          params: Object.assign(
            {
              tpl: 3,
              page: 'detail',
              date: date,
              topid: this.top.id
            },
            this.params
          )
        })
        .then(res => {
          if (res.data.code == 0) {
            let list = res.data.songlist
            let songlist = []
            list.forEach(element => {
              songlist.push({
                album: element.data.albumname,
                duration: element.data.interval,
                img: `https://api.bzqll.com/music/tencent/pic?id=${
                  element.data.songmid
                }&key=579621905`,
                mid: element.data.songmid,
                name: element.data.songname,
                singer: element.data.singer[0].name,
                url: `https://api.bzqll.com/music/tencent/url?key=579621905&id=${
                  element.data.songmid
                }&br=192`,
                id: element.data.songid
              })
            })
            this.songList = songlist
            return true
          } else {
            return false
          }
        })
    },
    getTopList() {
      if (!this.top.id) {
        this.$router.push({
          path: '/rank'
        })
      }
      if (!this.getTop(this.date)) {
        this.getTop(this.stage)
      }
    }
  },
  components: {
    MusicList
  }
}
</script>